﻿using System;
using System.Collections.Generic;
using System.Text;
using Server.Items;
using Server.Targeting;

namespace Server.Engines.Harvest
{
    /// <summary>
    /// Fournit des méthodes de base (et générique à tout system) pour la collecte (pêche, minage, coupe de bois, ...)
    /// </summary>
    public static class HarvestHelper
    {
        public static bool GetTargetDetails(Mobile from, Item tool, object toHarvest, out int tileID, out Map map, out Point3D loc)
        {
            if (toHarvest is Static && !((Static)toHarvest).Movable)
            {
                Static obj = (Static)toHarvest;

                tileID = (obj.ItemID & 0x3FFF) | 0x4000;
                map = obj.Map;
                loc = obj.GetWorldLocation();
            }
            else if (toHarvest is StaticTarget)
            {
                StaticTarget obj = (StaticTarget)toHarvest;

                tileID = (obj.ItemID & 0x3FFF) | 0x4000;
                map = from.Map;
                loc = obj.Location;
            }
            else if (toHarvest is LandTarget)
            {
                LandTarget obj = (LandTarget)toHarvest;

                tileID = obj.TileID & 0x3FFF;
                map = from.Map;
                loc = obj.Location;
            }
            else
            {
                tileID = 0;
                map = null;
                loc = Point3D.Zero;
                return false;
            }

            return (map != null && map != Map.Internal);
        }

        public static object GetLock(Mobile from, Item tool, Type system)
        {
            /* Here we prevent multiple harvesting.
             * 
             * Some options:
             *  - 'return tool;' : This will allow the player to harvest more than once concurrently, but only if they use multiple tools. This seems to be as OSI.
             *  - 'return system.GetType();' : This will disallow multiple harvesting of the same type. That is, we couldn't mine more than once concurrently, but we could be both mining and lumberjacking.
             *  - 'return typeof( HarvestSystem );' : This will completely restrict concurrent harvesting.
             */

            return tool;
        }

        public static Item Construct(Type type, Mobile from)
        {
            try { return Activator.CreateInstance(type) as Item; }
            catch { return null; }
        }

        public static bool Give(Mobile to, Item item, bool placeAtFeet)
        {
            if (to.PlaceInBackpack(item))
                return true;

            if (!placeAtFeet)
                return false;

            Map map = to.Map;

            if (map == null)
                return false;

            List<Item> atFeet = new List<Item>();

            foreach (Item obj in to.GetItemsInRange(0))
                atFeet.Add(obj);

            for (int i = 0; i < atFeet.Count; ++i)
            {
                Item check = atFeet[i];

                if (check.StackWith(to, item, false))
                    return true;
            }

            item.MoveToWorld(to.Location, map);
            return true;
        }

    }
}
